package org.csu.jpetstore_manage_system.service;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.csu.jpetstore_manage_system.domain.Category;
import org.csu.jpetstore_manage_system.domain.Inventory;
import org.csu.jpetstore_manage_system.domain.Item;
import org.csu.jpetstore_manage_system.domain.Product;
import org.csu.jpetstore_manage_system.persistence.CategoryMapper;
import org.csu.jpetstore_manage_system.persistence.InventoryMapper;
import org.csu.jpetstore_manage_system.persistence.ItemMapper;
import org.csu.jpetstore_manage_system.persistence.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("catalogService")
public class CatalogService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private ItemMapper itemMapper;
    @Autowired
    private InventoryMapper inventoryMapper;

    //模糊查找
    public List<Category> getCategoryByKeyWord(String keyword){
        UpdateWrapper<Category> categoryUpdateWrapper = new UpdateWrapper<>();
        categoryUpdateWrapper.like("name", keyword.toLowerCase());

        List<Category> categoryList = categoryMapper.selectList(categoryUpdateWrapper);
        return categoryList;
    }

    //查找category
    public Category getCategory(String categoryId) {
        Category category = categoryMapper.selectById(categoryId);
        return category;
    }

    //查找所有category
    public List<Category> getCategoryList() {
        List<Category> categoryList = categoryMapper.selectList(new UpdateWrapper<Category>());
        return categoryList;
    }

    public Boolean insertCategory(Category category) {
        int count = categoryMapper.insert(category);
        return count != 0;
    }

    public Boolean deleteCategory(String categoryId){
        int count = categoryMapper.deleteById(categoryId);
        //删除category下的所有product
        List<Product> productList = productMapper.selectList(new UpdateWrapper<Product>().eq("category", categoryId));
        for(Product product: productList){
            productMapper.deleteById(product);
            //删除product下的所有item
            List<Item> itemList = itemMapper.selectList(new UpdateWrapper<Item>().eq("productid", product.getProductId()));
            for(Item item: itemList){
                itemMapper.deleteById(item);
            }
        }
        return count != 0;
    }

    public Boolean updateCategory(Category category){
        int count = categoryMapper.updateById(category);
        return count != 0;
    }

    //查找product
    public Product getProduct(String productId) {
        Product product = productMapper.selectById(productId);
        return product;
    }

    //查找所有product
    public List<Product> getAllProductList() {
        List<Product> productList = productMapper.selectList(new UpdateWrapper<Product>());
        return productList;
    }

    //查找category下的所有product
    public List<Product> getProductListByCategory(String categoryId) {
        List<Product> productList = productMapper.selectList(new UpdateWrapper<Product>().eq("category", categoryId));
        return productList;
    }

    //通过关键字，模糊查询 product种类
    public List<Product> searchProductList(String keyword) {
        UpdateWrapper<Product> productUpdateWrapper = new UpdateWrapper<>();
        productUpdateWrapper.like("name", keyword.toLowerCase());

        List<Product> productList = productMapper.selectList( productUpdateWrapper);
        return productList;
    }

    //通过productId获取item组
    public List<Item> getItemListByProduct(String productId) {
        List<Item> itemList = itemMapper.selectList(new UpdateWrapper<Item>().eq("productid", productId));
        for (Item item: itemList) {
            setProductAndQty(item);
        }
        return itemList;
    }

    public void setProductAndQty(Item item){
        //通过item类中的productid从product表中获取 product对象 并放入item对象中
        Product product = productMapper.selectOne(new UpdateWrapper<Product>().eq("productid", item.getProductId()));
        item.setProduct(product);
        //通过item类中的itemid从inventory表中获取 quantity 并放入item对象中
        Inventory inventory = inventoryMapper.selectOne(new UpdateWrapper<Inventory>().eq("itemid", item.getItemId()));
        item.setQuantity(inventory.getQty());
    }

    public Item getItem(String itemId) {
        Item item = itemMapper.selectById(itemId);
        setProductAndQty(item);
        return item;
    }

    //更新item，同时需要更新inventory中的商品数量
    public boolean updateItem(Item item) {
        int count = itemMapper.updateById(item);

        Product product = item.getProduct();
        productMapper.updateById(product);

        //更新 inventory表，商品剩余量
        Inventory inventory = new Inventory();
        inventory.setItemId(item.getItemId());
        inventory.setQty(item.getQuantity());
        inventoryMapper.updateById(inventory);
        return count != 0;
    }

    //删除item同时要在inventory表中 删除
    public boolean deleteItem(String itemId) {
        int count = itemMapper.delete(new UpdateWrapper<Item>().eq("itemid", itemId));
        inventoryMapper.delete(new UpdateWrapper<Inventory>().eq("itemid", itemId));
        return count != 0;
    }

    public boolean insertItem(Item item) {
        int count = itemMapper.insert(item);

        Inventory inventory = new Inventory();
        inventory.setItemId(item.getItemId());
        inventory.setQty(item.getQuantity());

        inventoryMapper.insert(inventory);

        return count != 0;
    }

    public boolean updateProduct(Product product){
        int count = productMapper.updateById(product);
        return count != 0;
    }

    public boolean insertProduct(Product product){
        int count = productMapper.insert(product);
        return count != 0;
    }

    public boolean deleteProduct(String productId){
        int count = productMapper.deleteById(productId);
        return count != 0;
    }
}
